package com.hitqz.robot.biz.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.dto.*;
import com.hitqz.robot.api.business.entity.NetworkMappingEntity;
import com.hitqz.robot.biz.service.NetworkMappingService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 网络映射管理表
 *
 * @author robot
 * @date 2024-06-06 15:18:09
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/network")
@Tag(description = "network", name = "网络映射管理")
public class NetworkMappingController {

    //table并添加元素
    private final Map<String, Map<String, String>> mapping = new HashMap<>(8);

    private final NetworkMappingService networkMappingService;


    @PostConstruct
    private void init() {
        HashMap<String, String> externalIpMapping = new HashMap<>();
        externalIpMapping.put("ws://192.168.3.55:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.55:16454/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.55:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.55:16554/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.45:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.45:16454/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.45:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.45:16554/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.35:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.35:16454/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.35:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.35:16554/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.25:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.25:16454/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.25:8567/live?source=rtsp://admin:hbyt137258@192.168.100.165:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hbyt137258@192.168.3.25:16554/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.10:8568/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/101", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/101");
        externalIpMapping.put("ws://192.168.3.10:8568/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/201", "ws://10.18.240.1:9999/admin/websocket/live?source=rtsp://admin:hit159357@192.168.3.78:554/streaming/channels/201");
        //mapping.put("192.168.16.12", externalIpMapping);
        mapping.put("10.18.240.1", externalIpMapping);
        //zq
        mapping.put("192.168.3.10", Map.of("192.168.3.25:80", "192.168.3.25:164", "192.168.3.35:80", "192.168.3.35:164", "192.168.10.109:80", "192.168.3.100:800"));
        mapping.put("192.168.3.50", Map.of("192.168.3.25:80", "192.168.3.25:164", "192.168.3.35:80", "192.168.3.35:164", "192.168.10.109:80", "192.168.3.100:800"));
        mapping.put("192.168.3.100", Map.of("192.168.10.109:80", "192.168.3.100:800"));
    }


    @Operation(summary = "请求网络映射结果")
    @PostMapping(value = "mapping")
    public R<NetworkMappingDto> mapping(@RequestBody NetworkMappingDto dto) {
        String target = dto.getTarget();
        String host = dto.getHost();
        dto.setMapping(target);
        // 没有指向性的直接返回
        if (StrUtil.isBlank(host)) {
            return R.ok(dto);
        }
        //先从数据库查询
        NetworkMappingEntity one = networkMappingService.getOne(Wrappers.<NetworkMappingEntity>lambdaQuery()
                .eq(NetworkMappingEntity::getHost, host)
                .eq(NetworkMappingEntity::getTarget, target)
                .last("limit 1"));
        if (one != null && StrUtil.isNotBlank(one.getMapping())){
            dto.setMapping(one.getMapping());
            return R.ok(dto);
        }
        //没查到,使用写死的映射
        if (mapping.containsKey(host)) {
            Map<String, String> targetMapping = mapping.get(host);
            if (targetMapping.containsKey(target)) {
                dto.setMapping(targetMapping.get(target));
            }
        }
        return R.ok(dto);
    }


    @Operation(summary = "批量请求网络映射结果")
    @PostMapping(value = "ListMapping")
    public R<List<NetworkMappingDto>> ListMapping(@RequestBody List<NetworkMappingDto> dtoList) {
        for (NetworkMappingDto dto : dtoList) {
            String host = dto.getHost();
            String target = dto.getTarget();
            dto.setMapping(target);
            if (StrUtil.isBlank(host)) {
                continue;
            }
            //先从数据库查询
            NetworkMappingEntity one = networkMappingService.getOne(Wrappers.<NetworkMappingEntity>lambdaQuery()
                    .eq(NetworkMappingEntity::getHost, host)
                    .eq(NetworkMappingEntity::getTarget, target)
                    .last("limit 1"));
            if (one != null && StrUtil.isNotBlank(one.getMapping())){
                dto.setMapping(one.getMapping());
                continue;
            }
            //没查到,使用写死的映射
            if (mapping.containsKey(host)) {
                Map<String, String> targetMapping = mapping.get(host);
                if (targetMapping.containsKey(target)) {
                    dto.setMapping(targetMapping.get(target));
                }
            }
        }
        return R.ok(dtoList);
    }


    public static void main(String[] args) {
        //base64 encode
        String encode = Base64.encode("ws://192.168.3.55:8567/live?source=rtsp://admin:hbyt137258@192.168.100.164:554/streaming/channels/101");
        System.out.println(encode);
        System.out.println(Base64.decodeStr(encode));
    }


}